import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
//main.js统一导入axiso
import axios from 'axios'
//axios请统一前缀
axios.defaults.baseURL = 'http://localhost:8080/';
//axiso起个别名,$axiso,每个vue文件不用再import,直接使用this.$axiso
Vue.prototype.$axios = axios


// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  //前端localstorage有token,统一添加
  let token=window.localStorage.getItem("token");
  if(token){
    config.headers.token=token;
  }
  let refreshtoken=window.localStorage.getItem("refreshtoken");
  if(refreshtoken){
    config.headers.refreshtoken=refreshtoken;
  }
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  //统一处理将响应头的token存入localstorage
  let token= response.headers.token;
  if(token){
    window.localStorage.setItem("token",token);
  }    
  let refreshtoken= response.headers.refreshtoken;
  if(refreshtoken){
    window.localStorage.setItem("refreshtoken",refreshtoken);
  }    
  if(response.data.code==401){
    // alert('xxx');
    ElementUI.Message('先登录,'+response.data.data);
    // window.location.href='/login';
    router.push("/login");
  }
  if(response.data.code==403){
    // alert('xxx');
    ElementUI.Message('403,'+response.data.data);
  }
  return response;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
});


Vue.config.productionTip = false

Vue.use(ElementUI);

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
